package com.ruoyi.web.controller.app;

import com.ruoyi.app.domain.bo.AppFeedbackBo;
import com.ruoyi.app.domain.vo.AppFeedbackVo;
import com.ruoyi.app.service.IAppFeedbackService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.enums.BusinessType;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

/**
 * APP用户意见反馈
 *
 * @author heike
 * @date 2025-02-09
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/app/feedback")
public class AppFeedbackController extends BaseController {

    private final IAppFeedbackService iAppFeedbackService;
    private final static HashMap<String, String> FEED_BACK = new HashMap<>();

    @PostConstruct
    public void init() {
        FEED_BACK.put("1", "功能建议");
        FEED_BACK.put("2", "BUG反馈");
        FEED_BACK.put("3", "新需求");
        FEED_BACK.put("4", "界面建议");
        FEED_BACK.put("5", "内容建议");
        FEED_BACK.put("6", "其他");
    }

    /**
     * 获取意见反馈的类型
     *
     * @return
     */
    @GetMapping("type")
    public R<ArrayList<Object>> type() {
        ArrayList<Object> target = new ArrayList<>();
        for (String key :
            FEED_BACK.keySet()) {
            HashMap<String, String> one = new HashMap<>();
            one.put("id", key);
            one.put("name", FEED_BACK.get(key));
            target.add(one);
        }
        return R.ok(target);
    }

    /**
     * 查询APP用户意见反馈列表
     */
    @GetMapping("/list")
    public TableDataInfo<AppFeedbackVo> list(AppFeedbackBo bo, PageQuery pageQuery) {
        return iAppFeedbackService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取APP用户意见反馈详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<AppFeedbackVo> getInfo(@NotNull(message = "主键不能为空")
                                    @PathVariable Long id) {
        return R.ok(iAppFeedbackService.queryById(id));
    }

    /**
     * 新增APP用户意见反馈
     */
    @Log(title = "APP用户意见反馈", businessType = BusinessType.INSERT)

    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) AppFeedbackBo bo) {
        bo.setUserId(getUserId());
        return toAjax(iAppFeedbackService.insertByBo(bo) ? 1 : 0);
    }


    /**
     * 删除APP用户意见反馈
     *
     * @param ids 主键串
     */
    @Log(title = "APP用户意见反馈", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iAppFeedbackService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
